﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Ccit.OA.Scheduler
{
    public partial class scheduler_WorkDay_set : Ccit.Utils.Page
    {
        private DateTime dtDate = new DateTime();
        private DataRow dr;
        private int intYear;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dtDate = DateTime.Now;
                intYear = dtDate.Year;
                //代码修改
                //修改日期:2004-04-30
                //修改人员:刘扬
                //修改原因:调整了风格,结果年份和汉字始终不能放在同样高度,故调整代码
                ViewState["Year"] = intYear.ToString();
                lbTitle.Text = ViewState["Year"].ToString() + "年工作日设置";
                PageInit();
            }
        }

        private void PageInit()
        {
            int i;
            int j;
            int intCountDays;
            //int intYear;
            int lngFstDayOfMonth = 0;
            int lngDayCountOfMonth = 0;
            string[,][] arrDay = new string[12, 37][]; 
            string strDay;
            int tmpDayCount = 0;
            int tmpMonthCount = 0;
            string strRowTitle = "";
            //初始化数组
            //intYear = 2004 ;
            for (i = 0; i < 12; i++)
            {
                for (j = 0; j < 37; j++)
                {
                    arrDay[i, j] = new string[2] { "0", "0" };
                }
            }
            //设置每行日期
            for (i = 0; i < 12; i++)
            {
                //返回一个月有多少天
                lngDayCountOfMonth = GetDayCountOfMonth(intYear, i + 1);
                //返回每月的首日为星期几
                lngFstDayOfMonth = GetFistDayOfMonth(intYear, i + 1, 1);
                intCountDays = 1;
                for (j = lngFstDayOfMonth; j < lngDayCountOfMonth + lngFstDayOfMonth; j++)
                {
                    arrDay[i, j][0] = intCountDays.ToString();
                    intCountDays++;
                }
            }
            //设置每一天状态
            DataSet dtSet = new DataSet();
            SchedulerBLL Bll = new SchedulerBLL();
            dtSet = Bll.GetWorkDayMes("and datediff(year,SCH_A4_10,'" + intYear + "')=0");
            #region MyRegion
            //CDataAccess.ODCN cDAC = new CDataAccess.ODCNClass();
            //if (cDAC.GetDataBaseType == "0")
            //    sXml = comTab.GetList("datediff(year,WFL_160_COL_20,'" + intYear + "')=0", "WFL_160");
            //else if (cDAC.GetDataBaseType == "1")
            //    sXml = comTab.GetList("SUBSTR(WFL_160_COL_20,1,4)=" + intYear.ToString(), "WFL_160");
            //oDoc.LoadXml(sXml);
            //strReader = new StringReader(oDoc.DocumentElement.OuterXml);
            //xmlReader = new XmlTextReader(strReader);
            //dtSet.ReadXml(xmlReader);
            #endregion
          
            if (dtSet.Tables.Count > 0)
            {
                for (int rowCnt = 0; rowCnt < dtSet.Tables[0].Rows.Count; rowCnt++)
                {
                    dr = dtSet.Tables[0].Rows[rowCnt];
                    strDay = dr["SCH_A4_10"].ToString();
                    //strDayFlag = dr["WFL_160_COL_30"].ToString();
                    string[] flowId = new string[3];
                    flowId[0]="0";
                    flowId[1]="0";
                    flowId[2]="0";
                    //tmpMonthCount = int.Parse(strDay.Substring(5, 2).Replace("-",""));
                    if (strDay!="")
                    {
                        flowId = strDay.Split('-');
                        tmpDayCount = int.Parse(flowId[2]);
                        tmpMonthCount = int.Parse(flowId[1]);
                        //tmpDayCount = int.Parse(strDay.Substring(8, 2).Replace("-", ""));
                    }
                    #region
                    //else
                    //{
                    //    if (tmpMonthCount.ToString().Length == 1)
                    //    {
                    //        if (strDay.Length == 8)
                    //        {
                    //            tmpDayCount = int.Parse(strDay.Substring(6, 2).Replace("-", ""));
                    //        }
                    //        else
                    //        {
                    //            tmpDayCount = int.Parse(strDay.Substring(7, 2).Replace("-", ""));
                    //        }
                    //    }
                    //    else
                    //    {
                    //        if (strDay.Length == 10)
                    //        {
                    //            tmpDayCount = int.Parse(strDay.Substring(8, 2).Replace("-", ""));
                    //        }
                    //        else
                    //        {
                    //            tmpDayCount = int.Parse(strDay.Substring(7, 2).Replace("-", ""));
                    //        }
                    //    }
                    //}
                    #endregion
                    for (i = 0; i < 12; i++)
                    {
                        lngFstDayOfMonth = GetFistDayOfMonth(intYear, i + 1, 1);
                        if (i + 1 == tmpMonthCount)
                        {
                            for (j = 0; j < 37; j++)
                            {
                                if (j == tmpDayCount + lngFstDayOfMonth - 1)
                                {
                                    arrDay[i, j][1] = "1";
                                }
                            }
                        }
                    }
                }
            }
            //输出到页面
            //输出第一列标题月份
            for (i = 0; i < 12; i++)
            {
                switch (i)
                {
                    case 0:
                        strRowTitle = "一月";
                        break;
                    case 1:
                        strRowTitle = "二月";
                        break;
                    case 2:
                        strRowTitle = "三月";
                        break;
                    case 3:
                        strRowTitle = "四月";
                        break;
                    case 4:
                        strRowTitle = "五月";
                        break;
                    case 5:
                        strRowTitle = "六月";
                        break;
                    case 6:
                        strRowTitle = "七月";
                        break;
                    case 7:
                        strRowTitle = "八月";
                        break;
                    case 8:
                        strRowTitle = "九月";
                        break;
                    case 9:
                        strRowTitle = "十月";
                        break;
                    case 10:
                        strRowTitle = "十一月";
                        break;
                    case 11:
                        strRowTitle = "十二月";
                        break;
                }
                //HtmlTableRow tmpRow =new HtmlTableRow() ;
                TableRow tmpRow = new TableRow();
                tmpRow.CssClass = "listtitle";
                tmpRow.Width = Unit.Parse("100%");
                //HtmlTableCell tmpCell1=new HtmlTableCell();
                TableCell tmpCell1 = new TableCell();
                tmpCell1.CssClass = "listline";
                tmpCell1.Text = strRowTitle;
                tmpCell1.Wrap = false;
                tmpRow.Cells.Add(tmpCell1);
                //输出每行的TD
                for (j = 0; j < 37; j++)
                {
                    DateTime myDate = new DateTime();
                    myDate = DateTime.Now;
                    TableCell tmpCell = new TableCell();
                    tmpCell.Width = Unit.Parse("3%");
                    HtmlInputButton btCell = new HtmlInputButton();
                    if (arrDay[i, j][0].ToString() != "0")
                    {
                        btCell.ID = intYear.ToString() + "-" + (i + 1).ToString() + "-" + arrDay[i, j][0].ToString();
                        //btCell.CommandName = "update" ;
                        //btCell.CommandArgument = i.ToString() + "-" +arrDay[i,j][0].ToString();
                        //btCell.Command += new CommandEventHandler(this.btCell_Command);
                        //btCell.Click += new EventHandler(btCell_Click);
                        btCell.Attributes["onclick"] = "chg_flag(\"" + btCell.ID.ToString() + "\");";
                        btCell.Attributes["width"] = "100%";
                        //	tmpCell.Text=arrDay[i,j][0].ToString() ;
                        if (intYear > myDate.Year)
                        {
                            if (arrDay[i, j][1].ToString() == "1")
                                btCell.Attributes["class"] = "cur_red";
                            else
                                btCell.Attributes["class"] = "cur_blue";
                            btCell.Value = arrDay[i, j][0].ToString();
                            tmpCell.Controls.Add(btCell);
                        }
                        else if (intYear == myDate.Year)
                        {
                            if (i + 1 > myDate.Month)
                            {
                                if (arrDay[i, j][1].ToString() == "1")
                                    btCell.Attributes["class"] = "cur_red";
                                else
                                    btCell.Attributes["class"] = "cur_blue";
                                btCell.Value = arrDay[i, j][0].ToString();
                                tmpCell.Controls.Add(btCell);
                            }
                            else if (i + 1 == myDate.Month)
                            {
                                if (int.Parse(arrDay[i, j][0]) > myDate.Day)
                                {
                                    if (arrDay[i, j][1].ToString() == "1")
                                        btCell.Attributes["class"] = "cur_red";
                                    else
                                        btCell.Attributes["class"] = "cur_blue";
                                    btCell.Value = arrDay[i, j][0].ToString();
                                    tmpCell.Controls.Add(btCell);
                                }
                                else
                                {
                                    if (arrDay[i, j][1].ToString() == "1")
                                        tmpCell.CssClass = "past_red";
                                    else
                                        tmpCell.CssClass = "past_blue";
                                    tmpCell.Text = arrDay[i, j][0].ToString();
                                }
                            }
                            else
                            {
                                if (arrDay[i, j][1].ToString() == "1")
                                    tmpCell.CssClass = "past_red";
                                else
                                    tmpCell.CssClass = "past_blue";
                                tmpCell.Text = arrDay[i, j][0].ToString();
                            }
                        }
                        else
                        {
                            if (arrDay[i, j][1].ToString() == "1")
                                tmpCell.CssClass = "past_red";
                            else
                                tmpCell.CssClass = "past_blue";
                            tmpCell.Text = arrDay[i, j][0].ToString();
                        }
                    }
                    else
                    {
                        tmpCell.Text = "";
                        tmpCell.CssClass = "null_color";
                        //	tmpCell.Text=arrDay[i,j][0].ToString() ;
                    }
                    tmpRow.Cells.Add(tmpCell);
                }
                tbMain.Rows.Add(tmpRow);
            }
        }
        //返回每月的首日为星期几
        private int GetFistDayOfMonth(int iYear, int iMonth, int iDay)
        {
            int iFistDay = 0;
            DateTime myDate = new DateTime(iYear, iMonth, iDay);
            switch (myDate.DayOfWeek.ToString())
            {
                case "Saturday":
                    iFistDay = 0;
                    break;
                case "Sunday":
                    iFistDay = 1;
                    break;
                case "Monday":
                    iFistDay = 2;
                    break;
                case "Tuesday":
                    iFistDay = 3;
                    break;
                case "Wednesday":
                    iFistDay = 4;
                    break;
                case "Thursday":
                    iFistDay = 5;
                    break;
                case "Friday":
                    iFistDay = 6;
                    break;
            }
            return iFistDay;
        }
        //返回一个月有多少天
        private int GetDayCountOfMonth(int iYear, int iMonth)
        {
            int iDayCount = 0;
            switch (iMonth)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    iDayCount = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    iDayCount = 30;
                    break;
                case 2:
                    if ((iYear % 4 == 0 && iYear % 100 != 0) || iYear % 400 == 0)
                    {
                        iDayCount = 29;
                    }
                    else
                    {
                        iDayCount = 28;
                    }
                    break;
            }
            return iDayCount;
        }

        protected void imbPreYear_Click(object sender, ImageClickEventArgs e)
        {
            intYear = int.Parse(ViewState["Year"].ToString()) - 1;
            ViewState["Year"] = intYear.ToString();
            lbTitle.Text = ViewState["Year"].ToString() + "年工作日设置";
            PageInit();
        }

        protected void imbNextYear_Click(object sender, ImageClickEventArgs e)
        {
            intYear = int.Parse(ViewState["Year"].ToString()) + 1;
            ViewState["Year"] = intYear.ToString();
            lbTitle.Text = ViewState["Year"].ToString() + "年工作日设置";
            PageInit();
        }
        #region Ajax Methods
        [global::System.Web.Services.WebMethod]
        public static string DoOperateWorkDay(string strJson)
        {
            SchedulerBLL Bll = new SchedulerBLL();
            string iNum = Bll.DoOperateWorkDay(strJson); 
            Bll = null; 
            return iNum;
        }
        #endregion
    }
}
